package com.example.note2;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.litepal.LitePal;
import org.litepal.tablemanager.Connector;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView lvNote;
    private FloatingActionButton fab;
    private List<Note> notes;
    private ArrayAdapter<String> adapter;
    private ArrayList<String> texts = new ArrayList<>();
    private SQLiteDatabase db = Connector.getDatabase();
    private SimpleDateFormat format = new SimpleDateFormat("MM月dd日HH点MM分");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        notes = LitePal.findAll(Note.class);
        for (int i = 0; i < notes.size(); i++) {
            Note note = notes.get(i);
            texts.add((i + 1) + ". " + note.getText() + "——" + format.format(new Date(note.getTime())));
        }

        lvNote = findViewById(R.id.note_list);
        fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent addIntent = new Intent(MainActivity.this, EditActivity.class);
                startActivity(addIntent);
            }
        });

        fab.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                ConstraintLayout constraintLayout = findViewById(R.id.constraint_layout);
                constraintLayout.setBackground(getResources().getDrawable(R.drawable.background_black));
                Toast.makeText(MainActivity.this, "进入护眼模式", Toast.LENGTH_SHORT).show();
                return true;
            }
        });

        adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, texts);
        lvNote.setAdapter(adapter);
        lvNote.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent editIntent = new Intent(MainActivity.this, EditActivity.class);
                editIntent.putExtra("id", notes.get(position).getId());
                editIntent.putExtra("text", notes.get(position).getText());
                startActivity(editIntent);
            }
        });
        lvNote.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position,
                                           long id) {
                AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
                        .setTitle("删除")
                        .setMessage("确认要删除吗？")
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Note note = notes.get(position);
                                note.delete();
                                Toast.makeText(MainActivity.this, "已删除", Toast.LENGTH_SHORT).show();
                                texts.clear();
                                notes = LitePal.findAll(Note.class);
                                for (int i = 0; i < notes.size(); i++) {
                                    Note n = notes.get(i);
                                    texts.add((i + 1) + ". " + n.getText() + "——" + format.format(new Date(n.getTime())));
                                }
                                adapter.notifyDataSetChanged();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).create();
                dialog.show();
                return true;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        texts.clear();
        notes = LitePal.findAll(Note.class);
        for (int i = 0; i < notes.size(); i++) {
            Note note = notes.get(i);
            texts.add((i + 1) + ". " + note.getText() + "——" + format.format(new Date(note.getTime())));
        }
        adapter.notifyDataSetChanged();
    }
}
